/**
 * 设置token
 * @param token
 */
export function setToken(token) {
  localStorage.setItem('token', token);
}

/**
 * 获取token
 */
export function getToken() {
  return localStorage.getItem('token');
}

/**
 * 合并两个树结构
 * @param arg
 */
export function mergeTrees(...trees: any[][]) {
  const key = 'path';
  const map = new Map();

  function addNode(node: any) {
    if (!map.has(node[key])) {
      // Ensure we're not adding an empty children array to the final output
      const newNode = {
        ...node,
        children: node.children && node.children.length > 0 ? [] : undefined,
      };
      map.set(node[key], newNode);
    } else {
      const existingNode = map.get(node[key]);
      // Recursively merge children
      (node.children || []).forEach((child) => addNode(child));
      // Only merge children if they exist in both nodes
      if (existingNode.children !== undefined && node.children !== undefined) {
        existingNode.children = mergeTrees(existingNode.children, node.children, key);
      }
    }
  }

  trees.forEach((tree) => {
    tree.forEach((node) => addNode(node));
  });

  return Array.from(map.values()).filter((node) => node !== undefined);
}

/**
 * 文件根据指定大小切片
 */
export function sliceFile(file: File, chunkSize: number = 1 * 1024 * 1024): File[] {
  let index = 0;
  const fileSliceList: File[] = [];
  while (index < file.size) {
    let blob = file.slice(index, index + chunkSize);
    fileSliceList.push(blob as File);
    index += chunkSize;
  }
  return fileSliceList;
}

/**
 * 格式化文件大小
 * @param bits
 */
export function formatBits(bits: number): string {
  if (bits < 0) {
    throw new Error('Bits cannot be negative');
  }
  const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  let index = 0;
  let value = bits;
  while (value >= 1024 && index < units.length - 1) {
    value /= 1024;
    index++;
  }
  return `${value.toFixed(2)} ${units[index]}`;
}

// 生成一个UUID
export function getUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

// 判断文件是否为图片
export function isImage(fileType): boolean {
  const imageTypes = ['.jpg', '.png', '.jpeg', '.gif', '.bmp', '.webp'];
  return imageTypes.includes(fileType);
}

// 判断是否为视频
export function isVideo(fileType): boolean {
  const videoTypes = ['.mp4', '.avi', '.rmvb', '.mkv', '.flv', '.wmv'];
  return videoTypes.includes(fileType);
}

// 根据文件类型自动显示不同的图标
export function getIconByType(fileItem: { children: null | any[]; fileType: string }) {
  if (!fileItem.fileType) {
    return 'File';
  }

  const iconMap = {
    Excel: ['.xlsx', '.xls', '.csv'],
    Word: ['.docx', '.doc'],
    PPT: ['.pptx', '.ppt'],
    music: ['.mp3'],
    Pdf: ['.pdf'],
    tupian: ['.jpg', '.png', '.jpeg', '.gif', '.bmp', '.webp'],
    shipin: ['.mp4', '.avi', '.rmvb', '.mkv', '.flv', '.wmv'],
    txt: ['.txt'],
    Exe: ['.exe'],
    yasuobao: ['.zip', '.rar', '.7z'],
  };

  for (const [key, value] of Object.entries(iconMap)) {
    if (value.includes(fileItem.fileType)) {
      return key;
    }
  }

  return 'weizhiwenjian';
}

export function getCapacityList() {
  return [
    {
      label: '50MB',
      value: 52428800,
    },
    {
      label: '100MB',
      value: 104857600,
    },
    {
      label: '200MB',
      value: 209715200,
    },
    {
      label: '500MB',
      value: 524288000,
    },
    {
      label: '1GB',
      value: 1073741824,
    },
    {
      label: '2GB',
      value: 2147483648,
    },
    {
      label: '3GB',
      value: 3221225472,
    },
    {
      label: '4GB',
      value: 4294967296,
    },
    {
      label: '5GB',
      value: 5368709120,
    },
  ];
}
